using Microsoft.AspNetCore.Mvc;
using SharedKernel.Infrastructure.Comm;
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using UserService.Application.DTOs;
using UserService.Application.Services;
using UserService.Domain.Models;

namespace UserService.Controllers
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        /// <summary>
        /// 用户应用服务
        /// </summary>
        private readonly IUserAppService _userAppService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userAppService">用户应用服务</param>
        public UserController(IUserAppService userAppService)
        {
            _userAppService = userAppService;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost()]
        public async Task<ActionResult<ResponseInfo<UserLoginResInfo>>> Login([FromBody] UserLoginRequest request)
        {
            var resData = await _userAppService.UserLogin(request); 
            return new ActionResult<ResponseInfo<UserLoginResInfo>>(resData);
        }

        /// <summary>
        /// 获取用户
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <returns>用户信息</returns>
        [HttpGet("{id}")]
        public async Task<ActionResult<User>> GetUser(long id)
        {
            var user = await _userAppService.GetUserAsync(id);
            if (user == null)
                return NotFound();

            return user;
        }

        /// <summary>
        /// 根据用户名获取用户
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns>用户信息</returns>
        [HttpGet("by-username/{username}")]
        public async Task<ActionResult<User>> GetUserByUsername(string username)
        {
            var user = await _userAppService.GetUserByUsernameAsync(username);
            if (user == null)
                return NotFound();

            return user;
        }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="request">创建用户请求</param>
        /// <returns>创建结果</returns>
        [HttpPost]
        public async Task<ActionResult<Guid>> CreateUser([FromBody] CreateUserRequest request)
        {
            var userId = await _userAppService.CreateUserAsync(request);
            return CreatedAtAction(nameof(GetUser), new { id = userId }, userId);
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <param name="request">更新用户请求</param>
        [HttpPut("{id}")]
        public async Task<IActionResult> UpdateUser(long id, [FromBody] UpdateUserRequest request)
        {
            await _userAppService.UpdateUserAsync(id, request);
            return NoContent();
        }

        /// <summary>
        /// 更改密码
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <param name="request">更改密码请求</param>
        [HttpPut("{id}/change-password")]
        public async Task<IActionResult> ChangePassword(long id, [FromBody] ChangePasswordRequest request)
        {
            await _userAppService.ChangePasswordAsync(id, request.NewPasswordHash);
            return NoContent();
        }
    }

    /// <summary>
    /// 用户登录请求
    /// </summary>
    public class UserLoginRequest
    {
        [Display(Name = "手机号")]
        [Required(ErrorMessage = "请输入{0}")]
        [Phone(ErrorMessage = "{0}格式有误")]
        public required string Phone { get; set; }

        [Display(Name = "密码")]
        [Required(ErrorMessage = "请输入{0}")]
        public required string Password { get; set; }
    }

    /// <summary>
    /// 创建用户请求
    /// </summary>
    public class CreateUserRequest
    {
        /// <summary>
        /// 用户名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password{ get; set; }

        /// <summary>
        /// 电子邮件
        /// </summary>
        public string Email { get; set; }
    } 

    /// <summary>
    /// 更新用户请求
    /// </summary>
    public class UpdateUserRequest
    {
        /// <summary>
        /// 用户名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 电子邮件
        /// </summary>
        public string Email { get; set; }
    }

    /// <summary>
    /// 更改密码请求
    /// </summary>
    public class ChangePasswordRequest
    {
        /// <summary>
        /// 新密码哈希
        /// </summary>
        public string NewPasswordHash { get; set; }
    }
}